CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Using CKEditor 3.x [a.k.a FCKeditor] with jQuery in ASP.Net

By Satheesh Babu
Posted On Feb 12, 2010
Article Rating:
Average Rating: 4
No of Ratings: 1
No of Comments: 5
Category: ASP.Net
Print this article.

Using CKEditor 3.x [a.k.a FCKeditor] with jQuery in ASP.Net

 

CKEditor is formerly called as FCKEditor. It is an open source WYSIWYG editor which is called FCKEditor till 2.x version and got renamed to a new brand called CKEditor and continued its evolution from 3.0 version.

 

Visit the CKEditor official home page here. 

 

Read my articles on FCKEditor 2.x in codedigest below,

Integrating FCKeditor in ASP.Net Websites – Part 1

Integrating FCKeditor in ASP.Net Websites – Part 2

The new CKEditor includes all the features of FCKEditor and improved a lot in the areas like accessibility, performance and etc. Refer here to know more.

Also, the newer version of CKEditor supports the integration of the jQuery library by exposing some clientside API.

From an ASP.Net perspective, the CKEditor 3.x does not include a serverside .dll library but just clientside executables. Hence, an ASP.Net multiline textbox control can become a rich text editor in the client side by using CKEditor. We can also make a P tag or DIV tag as a rich text editor using the new CKEditor.

 

My previous article, Using CKEditor 3.x[aka FCKeditor] in ASP.Net discussed how to integrate and use the CKEditor 3.x without jQuery library.

 

Moving forward, we will see how the new CKEditor can be used with jQuery in an ASP.Net website.

Steps

1.      Create a new Asp.Net Website in your Visual Studio 2008.

2.      Download the latest CKEditor files from here. The current version is 3.1.

3.      Unzip the zip file. Copy the ckeditor folder into your visual studio solution. Refer below,

4.       You can see the jQuery adapter file in ckeditor/adapters/jquery.js. Integrate the latest the jQuery library downloaded from jquery.com. Read the following FAQ's to integrate jQuery library to your project.

What is jQuery? How to use it in ASP.Net Pages?

How to enable jQuery intellisense in Visual Studio 2008?

How to use jQuery intellisense in an external javascript file?

5.      Drag an ASP.Net textbox control into your aspx page. Set its TextMode property to MultiLine.

6.      To use the CKEditor features with the MultiLine textbox, we need to include ckeditor.js and jquery adapter file into our webpage.

Refer below,

  <script src="_scripts/jquery-1.3.2.min.js" type="text/javascript"></script>

 <script type="text/javascript" src="ckeditor/ckeditor.js"></script>

<script type="text/javascript" src="ckeditor/adapters/jquery.js"></script>

 

7.      Next, we need to call the ckeditor API to replace the mulitiline text box to an editor instance.

  $('#TextBox1').ckeditor();

 

The above can be called from jQuery ready event.

<script type="text/javascript" language="javascript">

        $(document).ready(function() {          

            $('#TextBox1').ckeditor();         

 

        });

   

    </script>

 

The HTML elements (P or DIV or textarea) will be replaced by an editor instance when the above code is executed. The content of the editor will be posted back to HTML element(textarea or P or DIV) when the page is submitted and it can be retrieved using element’s ID on the server side. Multiple ckeditors can be added by accessing the HTML elements through the above jQuery selector expression and calling the ckeditor() function.

8.      That’s it! Execute the page and you can see the CKEditor in action.

Refer below,




The final code will look like,

<head runat="server">

    <title></title>

 

    <script src="_scripts/jquery-1.3.2.min.js" type="text/javascript"></script>

    <script type="text/javascript" src="ckeditor/ckeditor.js"></script>

    <script type="text/javascript" src="ckeditor/adapters/jquery.js"></script> 

 

    <script type="text/javascript" language="javascript">

        $(document).ready(function() {          

            $('#TextBox1').ckeditor();

 

          });

   

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:TextBox ID="TextBox1" TextMode="MultiLine" runat="server"></asp:TextBox>   

    </div>

    <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />

    </form>

 

With this knowledge, we will move forward and do some basic customization on the CKEditor using jQuery.

 

Customizing CKEditor

By default, the CKeditor includes large number of editor options to do our rich text formatting. See the image above. Most of the time, we will not require all the above options to do our styling. We can customize the CKEditor to include only required options like we do in previous versions.

The ckeditor() function will accept 2 optional parameters. The first parameter is a callback function that gets executed when the editor is ready and second parameter is the configuration options. This configuration option will accept either inline set of styles or toolbarset name to render with the editor.

 

<script type="text/javascript" language="javascript">

        $(document).ready(function() {

            var options = {

                toolbar:

                      [

                         ['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-',

                'Link', 'Unlink'],

                         ['UIColor']

                      ]

            };

            $('#TextBox1').ckeditor(function() { /* callback code */ }, options);

 

 });

   

    </script>

 

OR

<script type="text/javascript" language="javascript">

        $(document).ready(function() {

 

            $('#TextBox1').ckeditor(function() {            var editor = $('#TextBox1').ckeditorGet();

        //Your code goes here

 }, {toolbar:'Basic'});

 

        });

   

    </script>

 

Execute the page and you can see the customized CKEditor in action.

Refer here to know about all available editor options. 

 

Fetching the Text from CKEditor

Since, it is an ASP.Net textbox control it can be accessed by regular Text property in the server. By default, ASP.Net will not allow HTML inputs to be posted to server in order to prevent the cross site scripting attach. You will get the following error,

A potentially dangerous Request.Form value was detected from the client (TextBox1="").

 

You need to set ValidateRequest="false" in the Page directive to prevent this error.

 

You can fetch the text from client side through val() method exposed by jQuery adapter.Refer below,

<script type="text/javascript" language="javascript">

        $(document).ready(function() {

 

        $('#TextBox1').ckeditor(function() {

            var editor = $('#TextBox1').ckeditorGet();

        //Your code goes here

         }, { toolbar: 'Basic'});

 

            $('#TextBox1').val("<b>I love CKEditor!!</b>");

            alert("jquery way" + $('#TextBox1').val());

       

        });

   

    </script>

 

 

Upload Images with CKeditor

To upload image and emebed it from CKeditor, please refer,

Upload Images and Integrate with CKeditor in Asp.Net

 

Working with Events

Apart from general events exposed by CKEditor, there are four events raised by the jQuery adapter.

 

instanceReady.ckeditor

fired when the editor is created, but before any callback being passed to the ckeditor() method.

setData.ckeditor

fired when data is set into the editor.

getData.ckeditor

fired when data is fetched from the editor. The current editor data is also passed in the arguments.

destroy.ckeditor

fired when the editor gets destroyed. It can be used, for example, to execute some cleanup on the page.

Syntax

$('#TextBox1').ckeditor(function() {/* callback code */

         }, { toolbar settings/configs,

            on: {

                event: eventmethodname

                }

            });

 

Example

<script type="text/javascript" language="javascript">

        $(document).ready(function() {

 

        $('#TextBox1').ckeditor(function() {

            var editor = $('#TextBox1').ckeditorGet();

        //Your code goes here

         }, { toolbar: 'Basic',

            on: {

                instanceReady: instanceReadyEvent

                }

            });

 

            function instanceReadyEvent(ev) {

                this.document.on("keyup", function() {

                    var editor = $('#TextBox1').ckeditorGet();

                    var tex = editor.getData();

                });

            }

 

        });

   

    </script>

In the above code, we have registered a keyup event using jQuery instanceReady event. Refer here to know more abovt editor instance.

 

Conclusion

Thus, we have seen how the new CKEditor can be used in an ASP.Net page with and without the jQuery library. Since, jQuery library is becoming more commonly used library, the CKEditor team understood its importance and exposed the API for jQuery access. Go ahead, try this cool editor you will definitely love this!!

 

Happy Coding!!

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
Grate !!
This is very nice code for CK editor . Realy nice!!
Thanks !!
Howabout Uploading file from CKEditor to ASP.NET
How can we enable CKEditor to upload files from CK Editor to server asp.net?
Thankyou
RE:setting ValidateRequest to false dangerous?
You have to set it to false to post HTML contents, thats the reason i have asked to set ValidateRequest to"false" only for that page and not in web.config for the whole application..
setting ValidateRequest to flase dangerous?
isnt it dangerous to set ValidateRequest to"false"?
Good Article
Hi friend, using CKEditor in asp.net using JQuery is very good concept. First of all thankyou very much.

But it is giving the following exception, whenever we use Update Panel in page. How we can overtake this exception:

System.Security.SecurityException: Request for the permission of type 'System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Thankyou